package com.spinach.game.core;

import com.spinach.game.enums.PokerLevel;
import com.spinach.game.enums.PokerType;
import com.spinach.game.enums.RoomStatus;
import io.netty.channel.Channel;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 斗地主房间
 */
@Data
public class Room {
    /**
     * 房间id
     */
    private Long id;

    /**
     * 倍数
     */
    private Integer multiplier = 1;

    /**
     * 基底分数
     */
    private final Integer basePoints = 100;

    /**
     * 房间名
     */
    private String name;

    /**
     * 房间状态
     */
    private RoomStatus roomStatus;

    /**
     * 房间人数
     */
    private AtomicInteger playerNum = new AtomicInteger(0);


    /**
     * 房间内玩家
     */
    private Map<Channel, GamePlayer> channelGamePlayerMap = new ConcurrentHashMap<>();

    /**
     * 地主牌
     */
    private List<Poker> landlordPokers;

    /**
     * 最近一次出牌的信息
     */
    private PokerSell lastPokerShell;

    /**
     * 地主玩家
     */
    private GamePlayer landlordPlayer;

    /**
     * 操作玩家,用于确定接下来由谁来操作
     */
    private GamePlayer operator;

    /**
     * 游戏玩家
     */
    private List<GamePlayer> players = new ArrayList<>(3);

    /**
     * 头指针
     */
    private GamePlayer head;

    /**
     * 尾指针
     */
    private GamePlayer tail;

    /**
     * 基础牌型
     */
    private List<Poker> basePokers = new ArrayList<>();

    /**
     * 重置
     */
    public void reset() {
        roomStatus = RoomStatus.WAIT;
        lastPokerShell = null;
        landlordPokers.clear();
        multiplier = 1;
    }

    public void initPokers() {
        for (PokerLevel pokerLevel : PokerLevel.values()) {
            if (pokerLevel.getLevel() >= PokerLevel.LEVEL_SMALL_KING.getLevel()) {
                Poker poker = Poker.builder()
                        .level(pokerLevel)
                        .type(PokerType.JOKER)
                        .build();
                basePokers.add(poker);
                continue;
            }
            for (PokerType pokerType : PokerType.values()) {
                if (pokerType == PokerType.JOKER) {
                    continue;
                }
                Poker poker = Poker.builder()
                        .level(pokerLevel)
                        .type(pokerType)
                        .build();
                basePokers.add(poker);
            }
        }
    }

}
